package org.tissou.client.Services;

import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Created by IntelliJ IDEA.
 * User: Manu
 * Date: 15 mars 2009
 * Time: 17:44:02
 * To change this template use File | Settings | File Templates.
 */
public class ArbreDeCategories extends Tree {
  private String categorieMere;

  private AsyncCallback<CategorieInfos> chargeageCallback = new AsyncCallback<CategorieInfos>() {

    public void onFailure(Throwable caught) {
      addItem("Erreur mec." + caught.getMessage());
    }

    public void onSuccess(CategorieInfos result) {
      TreeItem tout = result.donneLaStructureGWTTree();
      tout.setState(true);
      addItem(tout);
    }
  };

  public ArbreDeCategories() {
    this("Tout");
  }

  private void chargerLArbre() {
    CategorieService.App.getInstance().donneCategorie(categorieMere, chargeageCallback);
  }

  public void Refresh(){
      this.clear();
      chargerLArbre();
    }

  public ArbreDeCategories(String categorieMere) {
    super();
    this.categorieMere = categorieMere;
    chargerLArbre();
  }

  public CategorieInfos donneLaCategorieSelectionnee() throws MonException{
    try {
      return (CategorieInfos) this.getSelectedItem().getUserObject();
    } catch (NullPointerException e){
      throw new MonException("Tu n'as pas selectionne de categorie !", e);
    }
  }
}
